package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.utils.DateUtil;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 商场管理/品牌制造商模块的service层实现
 *
 * @Author: zhang
 * @Date: 2022/11/20 15:28
 * @Since: jdk1.8
 */
@Service
public class MarketBrandServiceImpl implements MarketBrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public CommonData<MarketBrand> list(BasePageInfo info, String id, String name) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        // 显示没有被逻辑删除的品牌
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        List<MarketBrand> marketBrandList = marketBrandMapper.selectByExample(example);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrandList);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketBrand create(MarketBrand marketBrand) throws SQLException {
        // 判断品牌商是否已经注册
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(marketBrand.getName());
        List<MarketBrand> marketBrandList = marketBrandMapper.selectByExample(example);
        if (marketBrandList.size() != 0) {
            return null;
        }
        Date date = DateUtil.getDate();
        marketBrand.setAddTime(date);
        marketBrand.setUpdateTime(date);
        marketBrand.setSortOrder(new Byte(StringUtil.getRandomNum(2)));
        marketBrand.setDeleted(false);
        int affectedRows = marketBrandMapper.insert(marketBrand);
        if (affectedRows == 0) {
            throw new SQLException("insert into market_brand failed");
        }
        return marketBrand;
    }

    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        Date date = DateUtil.getDate();
        marketBrand.setUpdateTime(date);
        marketBrandMapper.updateByPrimaryKey(marketBrand);
        return marketBrand;
    }

    @Override
    public int delete(MarketBrand marketBrand) {
        marketBrand.setDeleted(true);
        return marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
    }
}
